regexp help needed.

Need help what am I doing wrong int the 'regexp'?

 

pragma encoding, "UTF-8"
pragma runLim, 0

string s = "[tag: [DOORS]][tag: IBM]\n[tag: !@#$%^&*()[]{}]"

Regexp Tag = regexp("(\\[(.*?)\\])");

int from = 0
Buffer source = create (); source = s
while (Tag source) {

        int oStart = start 0
        int oEnd   = end 0
        Buffer x = source[match 0]
        print stringOf x "\n"
        delete x

        // clip the rest of the string
        Buffer y = source[oEnd:]
        delete source
        source = y
}

Output:
[tag: [DOORS]
[tag: IBM]
[tag: !@#$%^&*()[]

Expected Output:
[tag: [DOORS]]
[tag: IBM]
[tag: !@#$%^&*()[]]

 


EHcnck - Tue Sep 19 13:33:22 EDT 2017

Re: regexp help needed.
Mike.Scharnow - Tue Sep 19 14:50:18 EDT 2017

Tag = regexp "\\[[^[]*(\\[[^]]*\\])?[^]]*\\]"

should do the trick..

Output: 

[tag: [DOORS]]
[tag: IBM]
[tag: !@#$%^&*()[]{}]

That includes the curly brackets, but I think that's what you wanted

 

Explanation:

the expression starts with a mandatory real [, followed by any number of "is not a [".

Then you have an optional group (0 or 1 occurrences) of a real [, followed by any number of "not a [", followed by a real ].

This group is followed by any "not a ]" character, followed by a mandatory real ].

In case you really do not want to get the curly brackets, add more () groups and reference the correct output (match 1, match 2..)